import types from './types'
import {MessageBox} from 'element-ui'

export default {
  state: {
    listData: [
      {
        id: '1',
        title: '首页',
        name: '1',
        component: 'index',
        selMenu: '1'
      }
    ],
    tabIndex: '1',
    currentTab: '1',
    selMenu: '1',
    menuId: '',
    uid: {}
  },
  mutations: {
    [types.CURRENT_TAB] (state, data) {
      state.currentTab = data
    },
    [types.SEL_MENU] (state, data) {
      state.selMenu = data
    },
    [types.TAB_LIST_ADD] (state, data) {
      state.uid = data.uid
      var arr = state.listData
      var _tabIndex = JSON.stringify(parseInt(state.tabIndex) + 1)
      var tabs
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === data.menu_id) {
          tabs = true
          state.currentTab = arr[i].name
          state.indexData = arr[i].name
          state.menuId = arr[i].id
          // state.selMenu = arr[i].selMenu
          break
        }
      }
      if (!tabs) {
        if (state.listData.length === 10) {
          MessageBox.alert('您打开的页签太多了，请节约资源关闭不用的页签', '友情提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        } else {
          state.tabIndex = JSON.stringify(parseInt(state.tabIndex) + 1)
          let _data = {
            id: data.menu_id,
            title: data.menu_name,
            name: state.tabIndex,
            component: data.per_controller,
            selMenu: data.selMenu
          }
          state.indexData = _tabIndex
          state.currentTab = _tabIndex
          state.listData.push(_data)
          state.menuId = _data.id
          // console.log(_data)
          // console.log(typeof (_data.id))
          // if (!isNaN(_data.id)) {
          //   console.log(state.listData)
          // }
          this.selMenu = _data.selMenu
        }
      }
    },
    [types.TAB_LIST_DEL] (state, data) {
      var currentName = ''
      let tabs = state.listData
      if (typeof data === 'object' && !!data.id) {
        let id = data.id
        for (let index = 0; index < tabs.length; index++) {
          let tab = tabs[index]
          if (id + '' === tab.id) {
            currentName = tab.name
            break
          }
        }
      } else {
        currentName = data
      }
      if (currentName === state.currentTab) {
        for (let index = 0; index < tabs.length; index++) {
          let tab = tabs[index]
          if (currentName === tab.name) {
            let nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              state.currentTab = nextTab.name
            } else {
              state.currentTab = JSON.stringify(index)
            }
            state.listData.splice(index, 1)
          }
        }
      } else {
        if (typeof data === 'string') {
          for (let index = 0; index < tabs.length; index++) {
            let tab = tabs[index]
            if (data === tab.name) {
              state.listData.splice(index, 1)
            }
          }
        } else if (typeof data === 'object') {
          for (let index = 0; index < tabs.length; index++) {
            let tab = tabs[index]
            if (data.id + '' === tab.id) {
              state.listData.splice(index, 1)
            }
          }
        }
      }
    },
    [types.TAB_RESET] (state, data) {
      // console.log('sass')
      // console.log(data)
      if (data === 'reset') {
        state.listData = [
          {
            id: '1',
            title: '首页',
            name: '1',
            component: 'index'
          }
        ]
        state.tabIndex = '1'
        state.currentTab = '1'
        state.menuId = ''
      }
    }
  },
  actions: {
    currentTab ({commit}, data) {
      commit(types.CURRENT_TAB, data)
    },
    selMenu ({commit}, data) {
      commit(types.SEL_MENU, data)
    },
    addTab ({commit}, data) {
      commit(types.TAB_LIST_ADD, data)
    },
    delTab ({commit}, data) {
      commit(types.TAB_LIST_DEL, data)
    },
    resetTab ({commit}, data) {
      commit(types.TAB_RESET, data)
    }
  }
}
